package com.nature.ffmpegforandroid

import android.content.Context
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ScaleGestureDetector

/**
 *User: RandBII
 *Date: 2021/1/4
 *Description:
 */
class MyGLSurfaceView(context: Context, sets: AttributeSet) : GLSurfaceView(context, sets),
    ScaleGestureDetector.OnScaleGestureListener {

    //    缩放因子
    val TOUCH_SCALE_FACTOR = 180.0f / 320
    var mPreviousX = 0f
    var mPreviousY = 0f
    var mAngleX = 0
    var mAngleY = 0
    var mRatioWidth = 0
    var mRatioHeight = 0
    var mDetector: ScaleGestureDetector? = null

    var mPreScale = 1.0f
    var mCurScale = 1.0f
    var mLastMultiTouchTime = 0L
    var mOnGestureCallback: MyOnGestureCallback? = null

    init {
        mDetector = ScaleGestureDetector(context, this)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        if (width == 0 || height == 0) {
            setMeasuredDimension(width, height)
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth)
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height)
            }
        }

    }

    fun setAspectRatio(w: Int, h: Int) {
        mRatioHeight = h
        mRatioWidth = w
        requestLayout()
    }


    override fun onScale(detector: ScaleGestureDetector?): Boolean {
        val preSpan = detector?.previousSpan ?: 0F
        val curSpan = detector?.currentSpan ?: 0F
        if (curSpan < preSpan) { // 缩小
            mCurScale = mPreScale - (preSpan - curSpan) / 200
        } else {// 放大
            mCurScale = mPreScale + (curSpan - preSpan) / 200
        }
        mCurScale = Math.max(0.05f, Math.min(mCurScale, 80.0f))
        mOnGestureCallback?.onGesture(mAngleX, mAngleY, mCurScale)
        return false
    }

    override fun onScaleBegin(detector: ScaleGestureDetector?): Boolean {
        return true
    }

    override fun onScaleEnd(detector: ScaleGestureDetector?) {

        mPreScale = mCurScale
        mLastMultiTouchTime = System.currentTimeMillis()

    }


    interface MyOnGestureCallback {

        fun onGesture(xRotateAngle: Int, yRotateAngle: Int, scale: Float)

        fun onTouchLoc(touchX: Float, touchY: Float)
    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {

        if (event?.pointerCount == 1) {
            consumeClickEvent(event)
            val currentTimeMills = System.currentTimeMillis()
            if (currentTimeMills - mLastMultiTouchTime > 200) {
                val x = event.x
                val y = event.y
                when (event.action) {
                    MotionEvent.ACTION_MOVE -> {
                        val dx = x - mPreviousX
                        val dy = y - mPreviousY
                        mAngleX += (dx * TOUCH_SCALE_FACTOR).toInt()
                        mAngleY += (dy * TOUCH_SCALE_FACTOR).toInt()
                    }
                }
                mPreviousX = x
                mPreviousY = y

            }

        } else {
            mDetector?.onTouchEvent(event)
        }

        return true
    }

    private fun consumeClickEvent(event: MotionEvent) {
        var touchx = -1f
        var touchy = -1f

        when (event.action) {
            MotionEvent.ACTION_UP -> {
                touchx = event.x
                touchy = event.y
                mOnGestureCallback?.onTouchLoc(touchx, touchy)
            }
            else -> {

            }
        }

    }
}